package com.jic.manager.controller.manage.conf;


import com.jic.common.base.vo.RestResult;
import com.jic.conf.request.ChannelRequest;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.ChannelCategory;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.ChannelCategoryService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author  自己来认领
 * @Description:  渠道类目
 * @date 2020-04-09 14:26
 */
@RestController
@RequestMapping("/channelcategory")
@Slf4j
@ApiModel(value = "渠道类目")
public class ChannelCategoryController {

    @Autowired
    ChannelCategoryService channelCategoryService;

    /**
     * 查询
     * @param
     * @return
     */
    @ApiModelProperty(value = "查询类目")
    @ResponseBody
    @RequestMapping(value = "/queryChannelCategoryInfo")
    public RestResult queryChannelCategoryInfo(@RequestBody ChannelCategory channelRequest) throws Throwable {
        RestResult restResult=new RestResult();
        ChannelRequest channelCategory = BeanUtil.copy(channelRequest, ChannelRequest.class);
        try {
            List<ChannelCategory> page = channelCategoryService.query(channelRequest);
            restResult.setData(page);
            restResult=RestResult.success(restResult);

        } catch (Exception e) {
            log.error("error",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }

    /**
     * 保存
     * @param
     * @return
     */
    @ApiModelProperty(value = "保存类目")
    @ResponseBody
    @RequestMapping(value = "/saveChannelCategoryInfo")
    public RestResult saveChannelCategoryInfo(@RequestBody ChannelRequest channelRequest) throws Throwable {
        RestResult restResult=new RestResult();
        ChannelRequest channelCategory = BeanUtil.copy(channelRequest, ChannelRequest.class);
        try {
            Long id =channelRequest.getId();
             if (id !=null && id != 0){
                 long flag=channelCategoryService.update(channelCategory);
             }else {
                 long flag=channelCategoryService.insert(channelCategory);
             }
            restResult=RestResult.success(restResult);

        } catch (Exception e) {
            log.error("error",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }

}
